<?php
/**
 * tpshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: IT宇宙人 2015-08-10 $
 */ 
namespace WXAPI\Controller;
use Think\Controller;
class IndexController extends BaseController {
    public function index(){
        $this->display();
    }
 
    /*
     * 获取首页数据
     */
    public function home(){
        //获取轮播图
        $data = M('ad')->where('pid = 2  AND enabled=1')->field(array('ad_link','ad_name','ad_code','pid'))->cache(true,TPSHOP_CACHE_TIME)->select();
        //广告地址转换
        foreach($data as $k=>$v){        
            $data[$k]['ad_code'] = SITE_URL.$v['ad_code'];

        }
		$model = M("Navigation");          
		$menu = $model->order("id desc")->select(); 		
     
		foreach ($menu as $key=>$value){    
			$menu[$key]['ad_code'] = SITE_URL.$value['ad_code'];    
		}

		$basic=tpCache("basic");
		$is_show_store=$basic['app_test'];
		
		$basic_title=tpCache("shop_info");
		$shop_title=$basic_title['store_name'];
        //广告2
        //27,28,29,30,31
		$ad_pid = M('ad_position')->where(array("position_name"=>'微信小程序广告'))->find();
		$pid=$ad_pid['position_id'] ;
        $ad = M('ad')->where(array("pid"=>"$pid"))->select();
        foreach ($ad as $key=>$value)
        {
        	$ad[$key]['ad_code'] = SITE_URL.$value['ad_code'];
        }
        //获取大分类
        $category_arr = M('goods_category')->where('parent_id=0  and is_hot=1')->field('id,name')->cache(false,TPSHOP_CACHE_TIME)->select();
        $result = array();
        foreach($category_arr as $c){
            $cat_arr = getCatGrandson($c['id']);
            //获取商品
            //$sql = "select goods_name,goods_id,original_img,shop_price from __PREFIX__goods where  cat_id in (".implode(',',$cat_arr).") limit 4";
            //$goods = M()->query($sql);
            if($cat_arr)
                $cat_id = implode (',', $cat_arr);
            $goodsList = M('goods')->where("cat_id1 in($cat_id)  and  is_recommend=1  and is_on_sale=1")->limit(4)->cache(false,TPSHOP_CACHE_TIME)->getField("goods_id,goods_name,original_img,shop_price,market_price,sales_sum,click_count,store_id");
            foreach($goodsList as $k => $v){
				
				$store_id=$v["store_id"];
				$store_state=M('store')->where("store_id=$store_id")->getField("store_state");
				if($store_state==1){
					$v['original_img'] = SITE_URL.$v['original_img'];
					$c['goods_list'][] = $v;
				}
            }            
            $result[] = $c;
        }
    	$store_class = M('store')->where("store_recommend=1")->limit('0 , 5')->select();
    	foreach ($store_class as $k=>$v){
    		$store_class[$k]['store_logo'] = SITE_URL.$v['store_logo'];
    		$store_class[$k]['goods_num'] = M('goods')->where(array("store_id"=>$v['store_id']))->count();
    		$store_class[$k]['goods'] = M('goods')->where(array("store_id"=>$v['store_id']))->limit('0 , 4')->select();
    		foreach ($store_class[$k]['goods'] as $key=>$value){
    			$store_class[$k]['goods'][$key]['original_img'] = SITE_URL.$value['original_img'];
    		}
    		
    	}
        exit(json_encode(array('status'=>1,'msg'=>'获取成功','result'=>array('goods'=>$result,'ad'=>$data,"menu"=>$menu,"is_show_store"=>$is_show_store,'shop_title'=>$shop_title),"ad"=>$ad,"store_class"=>$store_class)));
    }
    
    /**
     * 获取服务器配置
     */
    public function getConfig()
    {
        $config_arr = M('config')->select();
        exit(json_encode(array('status'=>1,'msg'=>'获取成功','result'=>$config_arr)));
    }
    /**
     * 获取插件信息
     */
    public function getPluginConfig()
    {
        $data = M('plugin')->where("type='payment' OR type='login'")->select();
        $arr = array();
        foreach($data as $k=>$v){
            unset( $data[$k]['config']);
            unset( $data[$k]['config']);

            $data[$k]['config_value'] = unserialize($v['config_value']);
            if($data[$k]['type'] == 'payment'){
                $arr['payment'][] =  $data[$k];
            }
            if($data[$k]['type'] == 'login'){
                $arr['login'][] =  $data[$k];
            }
        }
        exit(json_encode(array('status'=>1,'msg'=>'获取成功','result'=>$arr ? $arr : '')));
    }
}